"""
@File    :  mms_client.py
@Author  :  LeeSen
@Date    :  2024-08-26
@Version :  Python 3.7.4
"""
import binascii
import socket
import time

from common_function import log_format_print, debug_log_format_print

MMS_pkt = {
    "Copt": "0300001611e00000000800c0010ac2020001c1020000",
    "Init": "030000bb02f0800db20506130100160102140200023302000134020001c19c318199a003800101a28191810400000001820400000001a423300f0201010604520100013004060251013010020103060528ca220201300406025101615e305c020101a0576055a107060528ca220203a20706052901876701a30302010ca606060429018767a70302010cbe2f282d020103a028a826800300fde881010a82010a830105a416800101810305f100820c03ee1c00000408000079ef18",
    "Conclude": "0300001602f0800100010061093007020103a0028b00",
    "Finish": "0300001902f0800910c10e610c300a020101a0056203800100"
}


class MmsClient:
    def __init__(self, target_ip, release_method):
        self.connected = False
        self.mms_client_socket = None
        self.target_info = (target_ip, 102)
        self.release_method = release_method

    def on_connect(self):
        try:
            self.mms_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.mms_client_socket.settimeout(10)
            self.mms_client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
            self.mms_client_socket.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 60 * 1000, 10 * 1000))
            self.mms_client_socket.connect(self.target_info)
            time.sleep(5)
            self.mms_client_socket.send(binascii.a2b_hex(MMS_pkt["Copt"]))
            rev_data = self.mms_client_socket.recv(2048)
            self.mms_client_socket.send(binascii.a2b_hex(MMS_pkt["Init"]))
            rev_data = self.mms_client_socket.recv(2048)

        except Exception as e:
            self.connected = False
            self.mms_client_socket.close()
            self.mms_client_socket = None
            return False
        else:
            self.connected = True
            return True

    def dis_connect(self):
        if not self.connected:
            return None
        try:
            if self.release_method:
                self.mms_client_socket.send(binascii.a2b_hex(MMS_pkt["Conclude"]))
                rev_data = self.mms_client_socket.recv(2048)
                self.mms_client_socket.send(binascii.a2b_hex(MMS_pkt["Finish"]))
                rev_data = self.mms_client_socket.recv(2048)

            self.mms_client_socket.close()
            self.mms_client_socket = None
        except Exception as e:
            self.mms_client_socket = None
            self.connected = False
            return False
        else:
            return True

    def send_mms_package(self, pkt_hex_data):
        try:
            if self.connected:
                self.mms_client_socket.send(binascii.a2b_hex(pkt_hex_data))
                rev_data = self.mms_client_socket.recv(2048)
            else:
                return False
        except Exception as e:
            self.mms_client_socket.close()
            self.mms_client_socket = None
            self.connected = False
            return False
        else:
            return True